<template>
  <header class="header">
    <h1>todos</h1>
    <input
      :disabled="list.length === 0"
      v-model="isAll"
      id="toggle-all"
      class="toggle-all"
      type="checkbox"
    />
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      v-model.trim="taskname"
      @keydown.enter="add"
    />
  </header>
</template>

<script>
export default {
  props: ["list"],
  data() {
    return {
      taskname: "",
    };
  },
  methods: {
    add() {
      if (this.taskname.length === 0) return alert("请输入任务名称!");
      // 将 taskname 传递给父组件
      this.$emit("addTask", this.taskname);
      // 清空文本框
      this.taskname = "";
    },
  },
  computed: {
    isAll: {
      set(val) {
        // console.log(val)
        this.list.forEach((item) => (item.isDone = val));
      },
      get() {
        // 根据小选框的选中状态来决定
        // 如果全部选中就返回 true
        // 只要有一个没选中就返回 false
        if (this.list.length === 0) return false;
        return this.list.every((item) => item.isDone);
      },
    },
  },
};
</script>